$(function() {
//Declare variable
var 	tips = $( ".validateTips" ),
		button_name="",
		url="a_main.php", 
		ID="",
		mode="",
		rowSelect="";
		
		LoadData(1,true);
	
		$("#btnBack").click(function(){
			history.back(-1);
		});
		//Show Loading bar			
		function showLoader(){
			var col=0;
			if($("#view").val()=='product')
				col=7;
			else 
				col=8;
				
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan="+col+" class='search-background'>"
						+"<label><img src='./images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};
 
		//b=false , don't gennerate paging
		function LoadData(page,b){
			$( "#resultData tbody tr" ).remove();
			//Show loading
			showLoader();
			//set criteria
			var branch=0;
 
	
			 //Post 			
			var data={};
			data={'m':'pos_history_d'
				,'date':$("#ipos").val()
				,'branch':$("#ibr").val()
				,'type':$("#view").val()
				,'page':page};
						
			$.post(url,data,function(data){
		  //alert(data);
 				var obj=eval("(" + data + ")");
				//hide loading
				hideLoader();			
				
				 
				 $("#iBranch").attr('innerHTML', obj['branch']);
				$("#iDate").attr('innerHTML', obj['date']);
				$("#iAmount").attr('innerHTML', obj['amount']);
				$("#iPayment").attr('innerHTML', obj['payment']);
				$("#iRemain").attr('innerHTML', obj['remain']);
				
				//set data to table
				$( "#resultData tbody" ).append(obj['data']);
				//set row 
				$("#rowCount").attr('innerHTML',obj['rows']);
				//set page
			if (b){
 
					if (parseInt(obj['totalpage'])>1){
						$("#paging_btn").show();
						setPaging(parseInt(obj['totalpage']));
					}else{
						$("#paging_btn").hide();
					}					
					 
				}
				//$("#"+page).css({'background-color' : '#006699'});
			});
			
		}	
	
	function setPaging(iTotal){
				 $("#paging_btn").paginate({
				count 		: iTotal,
				start 		: 1,
				display     : 10,
				border					: false,
				text_color  			: '#888',
				background_color    	: '#EEE',	
				text_hover_color  		: 'blue',
				background_hover_color	: '#CFCFCF',
				mouse					: 'press',
				onChange     			: function(page){
											LoadData(page,false);
										  }
			});
		}
});
	